package cn.xenosp.server.module.third_server.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.tools.systemInfo.RedisInfoUtil;
import cn.xenosp.server.common.tools.systemInfo.SystemInfoUtil;
import cn.xenosp.server.common.tools.systemInfo.dto.OSInfo;
import cn.xenosp.server.common.tools.systemInfo.dto.OSRuntimeInfo;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;

import java.util.Properties;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.service
 * 作者：   TongHui
 * 创建时间: 2024-08-25 18:19
 * 描述: TODO
 * 版本: 1.0
 */
@Service
public class SystemInfoService {


    /**
     * 运行系统信息
     * cpu
     * 内存
     * jvm
     * 硬盘
     * 网卡
     * @return
     * @throws Exception
     */
    public Result systemInfo() throws Exception {
        JSONObject jsonObject = new JSONObject();

        OSInfo osInfo = SystemInfoUtil.getSystemInfo();
        String bootTime = osInfo.getBootTime();
        String runTime = "";
        if (StrUtil.isNotEmpty(bootTime)) {
            DateTime start = DateUtil.parse(bootTime, DatePattern.NORM_DATETIME_PATTERN);
            runTime = DateUtil.formatBetween(start, DateUtil.date());
            osInfo.setBootTime(runTime);
        }

        jsonObject.set("osInfo",osInfo);
//        jsonObject.set("jvm", SystemInfoUtil.jvm());
        OSRuntimeInfo osRuntimeInfo = SystemInfoUtil.getOSRuntimeInfo();
        JSONObject jsonObject1 = JSONUtil.toBean(JSONUtil.toJsonStr(osRuntimeInfo),JSONObject.class);
        jsonObject.set("oSRuntimeInfo", jsonObject1);
        //1.CPU信息
        jsonObject1.set("cpuUsageStr",SystemInfoUtil.formatRate(osRuntimeInfo.getCpuUsage()));
        //2.内存信息
        //系统内存总量
        long total = osRuntimeInfo.getTotalMemory();
        long used = osRuntimeInfo.getUsedMemory();
        double usage = used * 1.0 / total;
        jsonObject1.set("totalStr",SystemInfoUtil.formatData(total));
        jsonObject1.set("usedStr",SystemInfoUtil.formatData(used));
        jsonObject1.set("usageStr",SystemInfoUtil.formatRate(usage));
        //可用虚拟总内存
        long swapTotal = osRuntimeInfo.getSwapTotalMemory();
        //已用虚拟内存
        long swapUsed = osRuntimeInfo.getSwapUsedMemory();
        jsonObject1.set("swapTotalStr",SystemInfoUtil.formatData(swapTotal));
        jsonObject1.set("swapUsedStr",SystemInfoUtil.formatData(swapUsed));

        return Result.OK("",jsonObject);
    }

    public Result redisinfo(){
        Properties redisInfo = RedisInfoUtil.getRedisInfo();
        redisInfo.setProperty("os", SystemInfo.getCurrentPlatform().getName());
        return Result.OK("",redisInfo);
    }
    public Result rediskeys(){
        return Result.OK("",RedisInfoUtil.getAllKeys());
    }

    public Result jvmInfo(){
        return Result.OK("",SystemInfoUtil.jvm());
    }

}
